package com.o2o.app;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.o2o.app.adapter.MyPagerAdapter;

public class MainActivity extends Activity {
	private ViewPager vp_order_seat;
	private RadioGroup osIndicators;
	private ArrayList<View> imageList = new ArrayList<View>();
	private int picindex = 0;

	private ArrayList<Drawable> bitmapLists = new ArrayList<Drawable>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.demo1);

		vp_order_seat = (ViewPager) findViewById(R.id.vp_order_seat);
		osIndicators = (RadioGroup) findViewById(R.id.rg_order_seat_indicators);

		BQApplication myApp = (BQApplication) getApplication();
		imageList.addAll(myApp.getImageList());
		
		bitmapLists.addAll(myApp.getBitmapLists());

		initGuideViewPager(imageList, osIndicators, vp_order_seat);
	}

	/*
	 * 初始化广告展示条
	 */
	private void initGuideViewPager(List<View> picViews, final RadioGroup rg,
			ViewPager viewpager) {
		initIndicators(picViews, rg);

		MyPagerAdapter mPagerAdapter = new MyPagerAdapter(picViews);

		viewpager.setAdapter(mPagerAdapter);
		viewpager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				picindex = position;
				RadioButton radioButton = (RadioButton) rg.getChildAt(position);
				radioButton.setChecked(true);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}
		});
	}

	/**
	 * 初始化小点点
	 */
	private void initIndicators(List<View> picViews, RadioGroup rg) {
		for (int i = 0; i < picViews.size(); i++) {
			LayoutInflater layoutInflater = getLayoutInflater();
			RadioButton rbIndicator = (RadioButton) layoutInflater.inflate(
					R.layout.page_indicator, rg, false);
			rg.addView(rbIndicator);
		}
		if (picViews.size() > 0) {
			RadioButton radioButton = (RadioButton) rg.getChildAt(0);
			radioButton.setChecked(true);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}
